/*
   Copyright 2007 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include "cpputils/TestUtils.hpp"
#include "cpputils/ept/ObjectPool.hpp"
#include "cpputils/ept/ThreadPool.hpp"
#include "cpputils/ept/Timer.hpp"

#include "ThreadPoolTester.hpp"
#include "TimerTester.hpp"

using namespace cpputils::ept;
using namespace cpputils::utils;

BOOST_AUTO_TEST_CASE ( TestObjectPool ) {

	ObjectPool<int>::SharedPtr pool ( new ObjectPool<int>(5,2));
	BOOST_REQUIRE_MESSAGE ( pool.get(), "Instance is null" );
	int* obj = pool->getObject();
	pool->returnObject ( obj );
	pool.reset();
}

BOOST_AUTO_TEST_CASE ( TestThreadPool ) {
	ThreadPool::SharedPtr poThreadPool ( new ThreadPool ( 2, 5 ) );

	ThreadPoolTester::SharedPtr poPoolTester ( new ThreadPoolTester() );
	//bool res = pfHandler1.functor.obj_ptr == pfHandler2.functor.obj_ptr;

	ThreadPool::TaskId nTaskId = poThreadPool->createTaskId();
	try {
		poThreadPool->schedule(
			nTaskId,
			boost::bind(&ThreadPoolTester::body, poPoolTester.get())
		);

		poThreadPool->schedule(
			nTaskId,
			boost::bind(&ThreadPoolTester::body, poPoolTester.get())
		);

		poThreadPool->wait(nTaskId);
		BOOST_REQUIRE_MESSAGE ( poPoolTester->getTestValue() == 2 , "Incorrect test value" );

		poThreadPool->schedule(
			nTaskId,
			boost::bind(&ThreadPoolTester::body, poPoolTester.get())
		);
		poThreadPool->cancel( nTaskId );
		poThreadPool->wait(nTaskId);

	}
	catch(std::exception&) {
		poThreadPool->destroyTaskId(nTaskId);
		throw;
	}
}

BOOST_AUTO_TEST_CASE ( TestTimer ) {
	Timer oTimer;
	TimerTester oTimerTester;
	Timer::TimerId nId = 1;

	//Delay 500 msec
	oTimer.schedule(
		nId++,
		500,
		boost::bind ( &TimerTester::body, &oTimerTester, _1 )
	);

	//Delay 5000 msec (5 sec)
	oTimer.schedule(
		nId++,
		5000,
		boost::bind ( &TimerTester::body, &oTimerTester, _1 )
	);

	// Waiting one second
	boost::this_thread::sleep(boost::posix_time::seconds(1));
	BOOST_REQUIRE_MESSAGE ( oTimerTester.getTestValue() == 1, "Timer tester value is incorrect" );
}

BOOST_AUTO_TEST_CASE ( TestTimerRefObject ) {
	Timer oTimer;
	TimerTester oTimerTester;
	TimerTester* poTesterParam = new TimerTester();

	Timer::TimerId nId = 1;

	//Delay 500 msec
	oTimer.schedule(
		nId++,
		500,
		boost::bind ( &TimerTester::body2, &oTimerTester, _1, _2 ),
		poTesterParam
	);

	//Delay 5000 msec (5 sec)
	oTimer.schedule(
		nId++,
		5000,
		boost::bind ( &TimerTester::body2, &oTimerTester, _1, _2 ),
		poTesterParam
	);

	poTesterParam->release();

	// Waiting one second
	boost::this_thread::sleep(boost::posix_time::seconds(1));
	BOOST_REQUIRE_MESSAGE ( oTimerTester.getTestValue() == 1, "Timer tester value is incorrect" );
}
